﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Opo.Perspective.Core
{
    public class RepositoryStub : IRepository<IPerspectiveEntity>
    {
        private List<IPerspectiveEntity> _repository;

        public RepositoryStub()
        {
            _repository = new List<IPerspectiveEntity>();
            for (int i = 0; i < 10; i++)
            {
                IPerspectiveEntity entity = new EntityStub()
                {
                    Date = DateTime.Now,
                    Flag = new Flag("Flag" + i.ToString()),
                    ID = Guid.NewGuid(),
                    IsRead = (i % 2 == 0) ? true : false,
                    Tags = new TagCollection()
                    {
                        new Tag("tag1"),
                        new Tag("tag2"),
                        new Tag("tag3")
                    },
                    Title = "Entity " + i.ToString()
                };
                _repository.Add(entity);
            }
        }

        public IPerspectiveEntity Add(IPerspectiveEntity entity)
        {
            _repository.Add(entity);
            return entity;
        }

        public IPerspectiveEntity Update(IPerspectiveEntity entity)
        {
            _repository.Find(e => e.ID == entity.ID).Date = entity.Date;
            _repository.Find(e => e.ID == entity.ID).Flag = entity.Flag;
            _repository.Find(e => e.ID == entity.ID).IsRead = entity.IsRead;
            _repository.Find(e => e.ID == entity.ID).Tags = entity.Tags;
            _repository.Find(e => e.ID == entity.ID).Title = entity.Title;
            return entity;
        }

        public IPerspectiveEntity Get(Guid id)
        {
            return _repository.FirstOrDefault(e => e.ID == id);
        }

        public IEnumerable<IPerspectiveEntity> GetAll()
        {
            return _repository;
        }

        public void Delete(IPerspectiveEntity entity)
        {
            _repository.RemoveAll(e => e.ID == entity.ID);
        }
    }

    public class EntityStub : IPerspectiveEntity
    {
        public Guid ID { get; set; }

        public string Title { get; set; }

        public string Summary { get { return String.Format("{0}: {1}", ID, Title); } }

        public DateTime Date { get; set; }

        public bool IsRead { get; set; }

        public IFlag Flag { get; set; }

        public TagCollection Tags { get; set; }
    }
}
